﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Data;

public partial class Software_Software : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadSoftwareDetails();

        }

    }

    private void loadSoftwareDetails()
    {
        int software_ID = Int32.Parse((String)Session["Software_ID"]);

        SoftwareDBConnection softwareConn = new SoftwareDBConnection();
        Software software = softwareConn.getSoftwareByID(software_ID);

        txtBrandName.Text = software.brand_Name;
        txtSerial.Text = software.serial_Key;
        txtSoftwareID.Text = software.software_ID.ToString();
        txtSoftwareName.Text = software.software_Name;
        txtUsageTerms.Text = software.usage_Terms;
        WarrantyDBConnection warrantyConn = new WarrantyDBConnection();
        Warranty warranty = warrantyConn.getWarrantyById(Int32.Parse(software.warranty_ID.ToString()));
        linkWarranty.Text = warranty.warranty_Status.ToString();

        loadInstalledSoftware(software.software_ID);
        lbAssetHeading.Text = "Software Asset: "+software.software_Name+"("+software.software_ID+")";
    }

    private void loadInstalledSoftware(int software_ID)
    {
        OleDbConnection myOleDbConnection = new OleDbConnection();
        //string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database.accdb;";
        myOleDbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;

        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

        myOleDbConnection.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT Computers.Computer_ID, Computer_Name, Install_Date, Update_Version FROM [Installed_Software] INNER JOIN [Computers] ON (Installed_Software.Computers_ID = Computers.Computer_ID)"
                                                  +"WHERE Software_ID = " + software_ID + ";", myOleDbConnection);
            
            //"SELECT Software.Software_ID, Software_Name, Brand_Name, Update_Date "
            //                                      + "FROM [Installed_Software] INNER JOIN [Software] ON (Software.Software_ID = Installed_Software.Software_ID)"
            //                                      + "WHERE Computers_ID =" + computer_ID + ";", myOleDbConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "CustomersTable1");

        installedSoftwareGridView.DataSource = ds;
        installedSoftwareGridView.DataBind();

        myOleDbConnection.Close();



    }


    protected void WarrantyLinkButton_Click(object sender, EventArgs e)
    {
        SoftwareDBConnection softwareConn = new SoftwareDBConnection();
        Software software = softwareConn.getSoftwareByID(Int32.Parse(txtSoftwareID.Text));
        
        Session["Warranty_ID"] = software.warranty_ID;
        Response.Redirect("../Warranty/Warranty.aspx", true);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int software_ID = Int32.Parse(txtSoftwareID.Text);

        SoftwareDBConnection softwareConn = new SoftwareDBConnection();
        Software software = softwareConn.getSoftwareByID(software_ID);

        softwareConn.delete(software);

        FeedBack feedback = new FeedBack();
        feedback.success = 1;
        feedback.lastPageURL = "Assets.aspx";
        feedback.message = "You have successfully deleted the asset: '" +
                            software.software_Name + " (" + software.software_ID + ")' of type software asset.";
        Session["Feedback"] = feedback;
        Server.Transfer("../Feedback.aspx");
    }



    protected void installedSoftwareGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = installedSoftwareGridView.Rows[e.NewSelectedIndex];

        Session["Computer_ID"] = row.Cells[1].Text;
        Response.Redirect("../Computers/Computers.aspx", true);
    }
    protected void installedSoftwareGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='Blue';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='Black';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.installedSoftwareGridView, "Select$" + e.Row.RowIndex);
        }
    }
}